package com.aji.winecommon.utills;

import com.aji.winepojo.entity.WineAdd;
import com.aji.winepojo.vo.WinePreVo;
import com.aji.winepojo.vo.WineVo;
import com.aji.winepojo.vo.WineVoPlus;
import com.csvreader.CsvReader;
import com.csvreader.CsvWriter;
import lombok.RequiredArgsConstructor;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CsvUtills {
    /**
     * 处理光谱文件
     */
    public static List<WineAdd>csvToList(MultipartFile multipartFile){
        List<WineAdd> wineAdds = new ArrayList<>();
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(multipartFile.getInputStream(), StandardCharsets.UTF_8));
            CsvReader csvReader = new CsvReader(reader);
            csvReader.readHeaders();

            while (csvReader.readRecord()){
                WineAdd wineAdd = new WineAdd();
                String line = csvReader.getRawRecord();
                String[] lineSplit = line.split(",");

                if (lineSplit.length < 2) {
                    System.err.println("跳过格式错误的行：" + line);
                    continue;
                }

                wineAdd.setWineName(lineSplit[0]);
                wineAdd.setWineClassName(lineSplit[1]);
                wineAdd.setAgingCondition(lineSplit[2]);
                wineAdd.setSpectrum(String.join(",",Arrays.copyOfRange(lineSplit,3,lineSplit.length)));

                System.out.println(wineAdd);
                wineAdds.add(wineAdd);
                System.out.println(wineAdds);
            }
            csvReader.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return wineAdds;
    }
    public static List<WinePreVo>csvToListTakeClass(String filePath)  {
        List<WinePreVo> wineAdds = new ArrayList<>();

        try {
            InputStream inputStream = Files.newInputStream(Paths.get(filePath));
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
            CsvReader csvReader = new CsvReader(reader);
            csvReader.readHeaders();

            while (csvReader.readRecord()){
                WinePreVo wineAdd = new WinePreVo();
                String line = csvReader.getRawRecord();
                String []lineSplit = line.split(",");
                wineAdd.setWineName(lineSplit[0]);
                wineAdd.setPreClass(lineSplit[lineSplit.length-1]);
                List<Float> listSpectrum = new ArrayList<>();
                String[] strings = Arrays.copyOfRange(lineSplit, 1, lineSplit.length - 1);
                for (String s:strings
                ) {
                    listSpectrum.add(Float.parseFloat(s));
                }
                wineAdd.setSpectrumList(listSpectrum);
                System.out.println(wineAdd);
                wineAdds.add(wineAdd);
                System.out.println(wineAdds);
            }
            csvReader.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return wineAdds;
    }
    public static void wCsv(String file, List<WineVo> wineVoTrainList,List<Float>x) throws IOException {

        CsvWriter csvWriter = new CsvWriter(file
                ,',', StandardCharsets.UTF_8);
        String [] title = new String[x.size()+2];
        title[0]="";
        for (int i = 0; i < x.size(); i++) {
            title[i+1] = String.valueOf(x.get(i));
        }
        title[x.size()+1]="classes";
        csvWriter.writeRecord(title);
        for (WineVo wineVoTrain : wineVoTrainList) {
            String[]spectrum = wineVoTrain.getSpectrum().split(",");
            String name = wineVoTrain.getWineName();
            String className = wineVoTrain.getClassName();
            String[]elementRow = new String[spectrum.length+2];
            System.arraycopy(spectrum,0,elementRow,1,spectrum.length);
            elementRow[elementRow.length-1]=className;
            elementRow[0]=name;
            csvWriter.writeRecord(elementRow);

        }
        csvWriter.close();
    }
}
